package com.finger.gulimall.ware.listener;

import java.io.IOException;
import com.finger.common.constant.MqOrderConstant;
import com.finger.common.constant.MqStockConstant;
import com.finger.gulimall.ware.entity.to.MqLockStockTo;
import com.finger.gulimall.ware.entity.to.OrderTo;
import com.finger.gulimall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName StockReleaseListener
 * @Description
 * @Author finger
 * @Date 2023/4/29 0:05
 * @Version 1.0
 */
@Service
public class StockReleaseListener {

	@Autowired
	WareSkuService wareSkuService;

	@RabbitListener(queues = MqStockConstant.STOCK_RELEASE_STOCK_QUEUE)
	public void stockRelease(MqLockStockTo stockTo, Message message, Channel channel) throws IOException {
		try {
			wareSkuService.unLockStock(stockTo);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (Exception e) {
			e.printStackTrace();
			channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
		}
	}
	@RabbitListener(queues = MqOrderConstant.ORDER_RELEASE_OTHER)
	public void stockRelease(OrderTo orderTo, Message message, Channel channel) throws IOException {
		try {
			wareSkuService.unLockStock(orderTo);
			channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
		} catch (Exception e) {
			e.printStackTrace();
			channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
		}
	}

}
